package cn.stylefeng.guns.modular.business.frequency.banner.config.activityMq;

import cn.stylefeng.guns.modular.business.custom.constant.PublicCommon;
import cn.stylefeng.guns.modular.business.custom.enums.BonusTypeEnum;
import cn.stylefeng.guns.modular.business.frequency.banner.config.Application;
import cn.stylefeng.guns.modular.business.frequency.orderbonus.entity.OrderBonus;
import cn.stylefeng.guns.modular.business.frequency.orderbonus.mapper.OrderBonusMapper;
import cn.stylefeng.guns.modular.business.frequency.ordersub.entity.OrderSub;
import cn.stylefeng.guns.modular.business.frequency.ordersub.mapper.OrderSubMapper;
import cn.stylefeng.guns.modular.business.frequency.userawardbase.entity.UserAwardBase;
import cn.stylefeng.guns.modular.business.frequency.userawardbase.mapper.UserAwardBaseMapper;
import cn.stylefeng.guns.modular.business.util.BusinessUtil;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jms.JmsException;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.concurrent.ExecutorService;

/**
 * @Description:
 * @Author jzy
 * @Date 2021/1/11 0011
 **/
@Slf4j
@Component
public class Consumer {

    @Resource
    private OrderBonusMapper orderBonusMapper;
    @Resource
    private OrderSubMapper orderSubMapper;
    @Resource
    private UserAwardBaseMapper userAwardBaseMapper;
    @Resource
    private BusinessUtil businessUtil;
    /*    @Resource
        private OrderPrepayMapper orderPrepayMapper;
        @Resource
        private OrderSubMapper orderSubMapper;
        @Resource
        private SmsMessage smsMessage;*/
    @Autowired
    @Qualifier(value = "crawlExecutorPool")
    private ExecutorService crawlExecutorPool;

    @JmsListener(destination = Application.ACTIVEMQ_PREFIX + "sms.queue")
    public void receive(String msg) {
        log.info("activemq接收到消息：{}", msg);
    }

    @JmsListener(destination = Application.ACTIVEMQ_PREFIX + "bonus.queue")
    public void receiveBonus(String msg) throws JmsException {
        log.info("activemq接收到奖金处理消息：{}", msg);
        OrderBonus orderBonus = JSONObject.parseObject(msg, OrderBonus.class);
        OrderSub orderSub = orderSubMapper.selectById(orderBonus.getOrderId());
        List<UserAwardBase> uab = userAwardBaseMapper.getByUserIdAndTypeList(orderSub.getTenantId());
        BigDecimal fee = BigDecimal.ZERO;
        if (uab != null && !uab.isEmpty()) {
            UserAwardBase userAwardBase = uab.get(0);
            if (userAwardBase.getRoyaltyType() == PublicCommon.接单数) {
                if (orderBonus.getType() == BonusTypeEnum.CREATE_ORDER.type) {
                    fee = userAwardBase.getCreateFee();
                } else if (orderBonus.getType() == BonusTypeEnum.ASSIGN_PICK.type) {
                    fee = userAwardBase.getPickFee();
                } else if (orderBonus.getType() == BonusTypeEnum.TRANSPORT.type) {
                    fee = userAwardBase.getTransportFee();
                } else if (orderBonus.getType() == BonusTypeEnum.DELIVERY.type) {
                    fee = userAwardBase.getDeliveryFee();
                } else {
                    return;
                }
            } else if (userAwardBase.getRoyaltyType() == PublicCommon.毛利点) {

            } else if (userAwardBase.getRoyaltyType() == PublicCommon.路线) {

                UserAwardBase base = userAwardBaseMapper.getByUserIdAndType(businessUtil.getTenantId(), orderSub.getTakeAreaCode());
                if (base != null) {
                    if (orderBonus.getType() == BonusTypeEnum.CREATE_ORDER.type) {
                        fee = base.getCreateFee();
                    } else if (orderBonus.getType() == BonusTypeEnum.ASSIGN_PICK.type) {
                        fee = base.getPickFee();
                    } else if (orderBonus.getType() == BonusTypeEnum.TRANSPORT.type) {
                        fee = base.getTransportFee();
                    } else if (orderBonus.getType() == BonusTypeEnum.DELIVERY.type) {
                        fee = base.getDeliveryFee();
                    } else {
                        return;
                    }
                }
            }
        }


        if (uab != null && fee.compareTo(BigDecimal.ZERO) == 1) {
            orderBonus.setStatus(2);
            orderBonus.setEffectTime(new Date());
            orderBonus.setFee(fee == null ? BigDecimal.ZERO : fee);
            orderBonus.setTenantId(orderSub.getTenantId());
            orderBonusMapper.insert(orderBonus);
        }

    }


}
